﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DacSanMienTayOBJ;
using System.Security.Cryptography;
using System.Text;
public partial class UserControl_ManagerAdmin : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void InsertAdmin()
    {
        try
        {
            string userName = Request["txtUserName"];
            string pass = Request["txtPassWord"];
            if (userName == "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Vui lòng nhập username')", true);
            }
            else
            {
                if (pass == "")
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Vui lòng nhập password')", true);
                }
                else
                {
                    Random rand = new Random();
                    int number = rand.Next(1000, 9999);
                    string hashKey = number.ToString();
                    string strPass = pass + hashKey;
                    string passCode = MaHoaMD5(strPass);

                    Admin admin = new Admin();
                    admin.UserName = userName;
                    admin.Password = passCode;
                    admin.HashKey = hashKey;
                    admin.IsVisible = 1;

                   bool rs= AdminBusiness.InsertAdmin(admin);
                   if (rs)
                   {
                       int managerID = AdminBusiness.GetManagerIDNewest();

                       try
                       {
                           string groupAdmin = Request["hdGroupAdmin"];
                           string[] arrGroupID = groupAdmin.Split(',');
                           if (arrGroupID.Length > 0)
                           {
                               for (int j = 0; j < arrGroupID.Length; j++)
                               {
                                   try
                                   {
                                       AccessRight accessRight = new AccessRight();
                                       accessRight.AdminID = managerID;
                                       accessRight.GroupID = int.Parse(arrGroupID[j]);
                                       bool rsAccessRight = AdminBusiness.InsertAccessRight(accessRight);
                                   }
                                   catch
                                   {

                                   }
                               }
                           }
                       }
                       catch
                       {

                       }
                       Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Thêm thành công admin')", true);
                   }
                   else
                   {
                       Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Thêm thất bại. Vui lòng liên hệ admin')", true);
                   }
                }
            }
        }
        catch(Exception ex)
        {
            Utilitys.WriteLog(ex.Message);
        }
    }

    private string MaHoaMD5(string pass)
    {
        try
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] data = Encoding.UTF8.GetBytes(pass);
            data = md5.ComputeHash(data);
            StringBuilder buider = new StringBuilder();
            foreach (byte b in data)
            {
                buider.Append(b.ToString("x2"));
            }
            return buider.ToString();
        }
        catch
        {
            return pass;
        }
    }

    protected void cmdInsert_Click(object sender, EventArgs e)
    {
        InsertAdmin();
    }
}